package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.business.services.BaseService;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.common.business.utils.WebClientUtil;
import com.dmcb.trade.controller.ArticleCrawlerController;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 洛阳新闻文章导入
 * Created by 黑米 on 2017/5/10.
 */
@Service
public class LuoYangCrawler extends BaseService{

    @Autowired
    private WebClientUtil webClientUtil;
    @Autowired
    private ArticleCrawlerController articleCrawlerController;
    org.slf4j.Logger logger = LoggerFactory.getLogger(LuoYangCrawler.class);

    public int getFist(int size){
        String sSize = ""+size;
        return Integer.parseInt(sSize.substring(0,1));
    }

    public JsonResult crawl(int size,int end) {
        int ero = 0;
        int success = 0;
        Document doc;
        String src;
        while (size > end) {
            int fist = getFist(size);
            src = "http://news.lyd.com.cn/system/count//0002001/001000000000/000/00"+fist+"/c0002001001000000000_00000"+size+".shtml";
            doc = webClientUtil.getDoc(src);
            if (doc == null) {
                return error("解析失败，链接："+src);
            }
            Elements elements = doc.select("span.style3");
            if (elements == null || elements.size() == 0) {
                return error("解析失败，链接："+src);
            }
            int star = 0;
            for (Element element : elements) {
                star++;
                if (star<2) {
                continue;
                }
                String link = element.select("a").first().attr("href");
                if (StringUtils.isBlank(link)) {
                    continue;
                }
                JsonResult jsonResult = articleCrawlerController.crawl(3667, link);
                if (jsonResult.getStatus() != JsonResult.STATUS_SUCCESS) {
                    if (jsonResult.getStatus() == JsonResult.STATUS_ERROR) {
                        ero++;
                    }
                    if (ero >= 10) {
                        return error("连续10次导入失败，请核实原因后再导入");
                    }
                    continue;
                }
                ero = 0;
                success++;
            }
            size--;
            if(size<1){
                break;
            }
        }
        return success("成功添加" + success + "条文章");
    }

}

